package com.lzx.adx.report.util.biz.product;

import cn.hutool.core.util.URLUtil;
import com.lzx.adx.report.dto.AccountDTO;
import com.lzx.adx.report.dto.ReportDTO;
import org.apache.commons.lang3.StringUtils;

import java.nio.charset.StandardCharsets;
import java.util.LinkedHashMap;
import java.util.Map;

public class WphChannelHelper {

    /**
     * 上报请求封装
     * @param reportDTO
     * @param accountDTO
     * @return
     */
    public static String covertReport(ReportDTO reportDTO, AccountDTO accountDTO) {
        Map<String,Object> params=new LinkedHashMap<>();

        if (StringUtils.isNotBlank(reportDTO.getOaid()) && !reportDTO.getOaid().contains("__")) {
            params.put("oaid", reportDTO.getOaid());
        }
        if(StringUtils.isNotBlank(reportDTO.getOaidSum()) && !reportDTO.getOaidSum().contains("__")) {
            params.put("oaid_md5", reportDTO.getOaidSum());
        }

        if(StringUtils.isNotBlank(reportDTO.getImei()) && !reportDTO.getImei().contains("__")) {
            params.put("imei", reportDTO.getImei());
        }
        if(StringUtils.isNotBlank(reportDTO.getImeiSum()) && !reportDTO.getImeiSum().contains("__")) {
            params.put("imei_md5", reportDTO.getImeiSum());
        }


        params.put("device_type", 2);
        params.put("click_time",String.valueOf(System.currentTimeMillis()));
        params.put("click_id",reportDTO.getBizRequestId());
        params.put("click_ip",reportDTO.getIp());
        params.put("user_agent",reportDTO.getUser_agent());

        String callBackUrl = String.format(accountDTO.getDspCallbackUrl(), reportDTO.getBizRequestId());
        params.put("callback_url",callBackUrl);

        String parms = URLUtil.buildQuery(params, StandardCharsets.UTF_8);
        return accountDTO.getDspReportUrl()+"&"+parms;
    }

}
